<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="批次号" prop="batchNo">
        <el-input
          v-model="queryParams.batchNo"
          placeholder="请输入批次号"
          clearable
          style="width: 240px"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料/产品编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入物料/产品编码"
          clearable
          style="width: 240px"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-tabs v-model="activeTab" type="border-card" @tab-click="handleTabClick">
      <el-tab-pane label="批次信息" name="batchInfo">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="批次号">{{ batchInfo.batchNo || '-' }}</el-descriptions-item>
          <el-descriptions-item label="物料/产品编码">{{ batchInfo.itemCode || '-' }}</el-descriptions-item>
          <el-descriptions-item label="物料/产品名称">{{ batchInfo.itemName || '-' }}</el-descriptions-item>
          <el-descriptions-item label="批次数量">{{ batchInfo.quantity || '-' }}</el-descriptions-item>
          <el-descriptions-item label="生产日期">{{ batchInfo.productionDate || '-' }}</el-descriptions-item>
          <el-descriptions-item label="有效期至">{{ batchInfo.expiryDate || '-' }}</el-descriptions-item>
          <el-descriptions-item label="批次状态">
            <el-tag :type="batchInfo.status === '1' ? 'success' : 'danger'">
              {{ batchInfo.status === '1' ? '正常' : '异常' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="创建时间">{{ batchInfo.createTime || '-' }}</el-descriptions-item>
        </el-descriptions>
      </el-tab-pane>
      
      <el-tab-pane label="质检记录" name="inspectionRecords">
        <el-table v-loading="loading" :data="inspectionList">
          <el-table-column label="检验单号" align="center" prop="inspectionCode" />
          <el-table-column label="检验类型" align="center" prop="inspectionType">
            <template slot-scope="scope">
              {{ inspectionTypeFormat(scope.row) }}
            </template>
          </el-table-column>
          <el-table-column label="检验结果" align="center" prop="result">
            <template slot-scope="scope">
              <el-tag :type="scope.row.result === '1' ? 'success' : 'danger'">
                {{ scope.row.result === '1' ? '合格' : '不合格' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="检验时间" align="center" prop="inspectionDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.inspectionDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleViewInspection(scope.row)"
              >查看</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      
      <el-tab-pane label="流转记录" name="flowRecords">
        <el-timeline>
          <el-timeline-item
            v-for="(activity, index) in flowRecords"
            :key="index"
            :timestamp="activity.timestamp"
            :type="activity.type"
            :color="activity.color"
            :size="activity.size"
            :icon="activity.icon"
          >
            {{ activity.content }}
          </el-timeline-item>
        </el-timeline>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
export default {
  name: "LotTraceability",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 激活的标签页
      activeTab: 'batchInfo',
      // 批次信息
      batchInfo: {},
      // 检验记录列表
      inspectionList: [],
      // 流转记录
      flowRecords: [],
      // 检验类型选项
      inspectionTypeOptions: [
        { dictLabel: '来料检验', dictValue: '0' },
        { dictLabel: '过程检验', dictValue: '1' },
        { dictLabel: '出货检验', dictValue: '2' },
        { dictLabel: '退料检验', dictValue: '3' }
      ],
      // 查询参数
      queryParams: {
        batchNo: undefined,
        itemCode: undefined
      }
    };
  },
  created() {
    // 如果URL中有批次号参数，则自动查询
    const batchNo = this.$route.query.batchNo;
    if (batchNo) {
      this.queryParams.batchNo = batchNo;
      this.handleQuery();
    }
  },
  methods: {
    /** 查询批次信息 */
    handleQuery() {
      if (!this.queryParams.batchNo && !this.queryParams.itemCode) {
        this.$modal.msgError("请输入批次号或物料/产品编码");
        return;
      }
      
      this.loading = true;
      // 模拟数据，实际应从后端获取
      setTimeout(() => {
        // 批次信息
        this.batchInfo = {
          batchNo: 'B20230701',
          itemCode: 'M001',
          itemName: '钢板',
          quantity: 100,
          productionDate: '2023-07-01',
          expiryDate: '2024-07-01',
          status: '1',
          createTime: '2023-07-01 10:00:00'
        };
        
        // 检验记录
        this.inspectionList = [
          {
            inspectionCode: 'LRJY20230701001',
            inspectionType: '0',
            result: '1',
            inspectionDate: new Date('2023-07-01 11:30:00')
          },
          {
            inspectionCode: 'GCJY20230702001',
            inspectionType: '1',
            result: '1',
            inspectionDate: new Date('2023-07-02 14:20:00')
          },
          {
            inspectionCode: 'CHJY20230703001',
            inspectionType: '2',
            result: '1',
            inspectionDate: new Date('2023-07-03 16:45:00')
          }
        ];
        
        // 流转记录
        this.flowRecords = [
          {
            timestamp: '2023-07-01 10:00:00',
            content: '批次创建',
            type: 'primary',
            color: '#0bbd87',
            size: 'normal',
            icon: 'el-icon-plus'
          },
          {
            timestamp: '2023-07-01 11:30:00',
            content: '来料检验完成，结果：合格',
            type: 'success',
            color: '#0bbd87',
            size: 'normal',
            icon: 'el-icon-check'
          },
          {
            timestamp: '2023-07-01 14:00:00',
            content: '入库，仓库：原材料仓，库位：A-01-01',
            type: 'info',
            color: '#409EFF',
            size: 'normal',
            icon: 'el-icon-box'
          },
          {
            timestamp: '2023-07-02 09:30:00',
            content: '领料出库，工单号：WO20230702001',
            type: 'info',
            color: '#409EFF',
            size: 'normal',
            icon: 'el-icon-shopping-cart-full'
          },
          {
            timestamp: '2023-07-02 14:20:00',
            content: '过程检验完成，结果：合格',
            type: 'success',
            color: '#0bbd87',
            size: 'normal',
            icon: 'el-icon-check'
          },
          {
            timestamp: '2023-07-03 16:45:00',
            content: '出货检验完成，结果：合格',
            type: 'success',
            color: '#0bbd87',
            size: 'normal',
            icon: 'el-icon-check'
          },
          {
            timestamp: '2023-07-04 10:30:00',
            content: '出库发货，发货单号：FH20230704001',
            type: 'warning',
            color: '#E6A23C',
            size: 'normal',
            icon: 'el-icon-truck'
          }
        ];
        
        this.loading = false;
      }, 300);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.batchInfo = {};
      this.inspectionList = [];
      this.flowRecords = [];
    },
    // 检验类型字典翻译
    inspectionTypeFormat(row) {
      return this.selectDictLabel(this.inspectionTypeOptions, row.inspectionType);
    },
    // 字典翻译
    selectDictLabel(datas, value) {
      if (value === undefined) {
        return "";
      }
      var actions = [];
      Object.keys(datas).some(key => {
        if (datas[key].dictValue == ('' + value)) {
          actions.push(datas[key].dictLabel);
          return true;
        }
      })
      return actions.join('');
    },
    /** 标签页切换 */
    handleTabClick(tab) {
      this.activeTab = tab.name;
    },
    /** 查看检验记录 */
    handleViewInspection(row) {
      const inspectionType = row.inspectionType;
      const inspectionCode = row.inspectionCode;
      
      // 根据检验类型跳转到不同的检验单详情页
      if (inspectionType === '0') {
        this.$router.push({ path: `/sb/IncomingInspection/detail/${inspectionCode}` });
      } else if (inspectionType === '1') {
        this.$router.push({ path: `/sb/ProcesInspection/detail/${inspectionCode}` });
      } else if (inspectionType === '2') {
        this.$router.push({ path: `/sb/OutgoingInspection/detail/${inspectionCode}` });
      } else if (inspectionType === '3') {
        this.$router.push({ path: `/sb/ReturnInspection/detail/${inspectionCode}` });
      }
    }
  }
};
</script>

<style scoped>
.el-timeline {
  padding: 20px;
}
</style> 